// Package admin
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2023 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
package admin

import (
	"context"
	"fmt"
	"hotgo/internal/consts"
	"hotgo/internal/dao"
	"hotgo/internal/model/input/apiin"
	"hotgo/internal/service"
	"time"
)

type sApiOperation struct{}

func init() {
	service.RegisterApiOperation(NewApiOperation())
}

func NewApiOperation() *sApiOperation {
	return &sApiOperation{}
}

// BannersList 获取广告信息
func (s *sApiOperation) BannersList(ctx context.Context) (list []*apiin.BannersListModel, err error) {
	list = make([]*apiin.BannersListModel, 0)
	err = dao.OpsBanners.Ctx(ctx).
		Where("status", 1).
		Where("start_time <= ?", time.Now()).
		Order("id desc").
		Scan(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// ProductNewList 新人商品列表
func (s *sApiOperation) ProductNewList(ctx context.Context) (list []*apiin.ProductNewListModel, err error) {
	err = dao.ProductNew.Ctx(ctx).Fields(
		dao.ProductNew.Table()+".id",
		dao.Product.Table()+".name",
		dao.Product.Table()+".main_image",
		dao.Product.Table()+".benefit_points",
		dao.Product.Table()+".market_price",
		dao.ProductNew.Table()+".new_price",
	).
		LeftJoin(dao.Product.Table(), dao.Product.Table()+".id="+dao.ProductNew.Table()+".product_id").
		Where(dao.Product.Table()+".status", 1).
		Where(dao.ProductNew.Table()+".status", 1).
		Order(dao.Product.Table() + ".click desc").
		Scan(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// ExchangeHistory 新人商品历史兑换消息
func (s *sApiOperation) ProductNewExchangeHistory(ctx context.Context) (list []*apiin.ProductNewExchangeHistoryModel, err error) {
	err = dao.Order.Ctx(ctx).Fields(
		dao.Product.Table()+".name",
		fmt.Sprintf("CONCAT(SUBSTRING(%s, 1, 1), '***') AS nickname", dao.AdminMember.Table()+".nickname"),
	).
		LeftJoin(dao.ProductNew.Table(), dao.Order.Table()+".product_new_id="+dao.ProductNew.Table()+".id").
		LeftJoin(dao.Product.Table(), dao.ProductNew.Table()+".product_id="+dao.Product.Table()+".id").
		LeftJoin(dao.AdminMember.Table(), dao.Order.Table()+".user_id="+dao.AdminMember.Table()+".id").
		WhereNotNull(dao.Order.Table() + ".product_new_id").
		Where(dao.AdminMember.Table() + ".nickname != ''").
		Where(dao.Product.Table() + ".name != ''").
		Order(dao.Order.Table() + ".created_at desc").
		Limit(20).
		Scan(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// 团购商品历史兑换消息
func (s *sApiOperation) ProductGroupExchangeHistory(ctx context.Context) (list []*apiin.ProductGroupExchangeHistory, err error) {
	err = dao.Order.Ctx(ctx).Fields(
		fmt.Sprintf("CONCAT(SUBSTRING(%s, 1, 1), '***', SUBSTRING(%s, CHAR_LENGTH(%s), 1)) AS name", dao.Product.Table()+".name", dao.Product.Table()+".name", dao.Product.Table()+".name"),
		fmt.Sprintf("CONCAT(SUBSTRING(%s, 1, 1), '***', SUBSTRING(%s, CHAR_LENGTH(%s), 1)) AS nickname", dao.AdminMember.Table()+".nickname", dao.AdminMember.Table()+".nickname", dao.AdminMember.Table()+".nickname"),
		dao.AdminMember.Table()+".avatar",
	).
		LeftJoin(dao.Product.Table(), dao.Order.Table()+".product_id="+dao.Product.Table()+".id").
		LeftJoin(dao.AdminMember.Table(), dao.Order.Table()+".user_id="+dao.AdminMember.Table()+".id").
		Where(dao.Order.Table()+".order_type", consts.OrderTypeGroupOrder).
		Where(dao.AdminMember.Table() + ".nickname != ''").
		Where(dao.AdminMember.Table() + ".avatar != ''").
		Where(dao.Product.Table() + ".name != ''").
		Order(dao.Order.Table() + ".created_at desc").
		Limit(20).
		Scan(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// 查询会员卡信息
func (s *sApiOperation) MemberCard(ctx context.Context, show string) (list []*apiin.MemberCard, err error) {
	mod := dao.MemberCard.Ctx(ctx)
	switch show {
	default:
		mod = mod.Where(dao.MemberCard.Columns().Type, consts.AdminMemberIdentityMember)
		//mod = mod.WhereIn(dao.MemberCard.Columns().Type, []int{consts.AdminMemberIdentityXiaoke, consts.AdminMemberIdentityService})
	case "agent":
		mod = mod.WhereIn(dao.MemberCard.Columns().Type, []int{consts.AdminMemberIdentityXiaoke, consts.AdminMemberIdentityService})
	case "all":
	}

	err = mod.Where(dao.MemberCard.Columns().Status, 1).OrderAsc("id").Scan(&list)
	if err != nil {
		return nil, err
	}
	if list == nil {
		list = []*apiin.MemberCard{}
	}
	return
}

// 获取指定类型的会员卡信息
func (s *sApiOperation) GetMemberCardOneByType(ctx context.Context, cardType int) (res *apiin.MemberCard, err error) {
	err = dao.MemberCard.Ctx(ctx).
		Where("card_type", cardType).
		Scan(&res)
	if err != nil {
		return nil, err
	}
	return
}

// 查询指定会员卡信息
func (s *sApiOperation) MemberCardDetail(ctx context.Context, in *apiin.MemberCardDetailInp) (res *apiin.MemberCard, err error) {
	err = dao.MemberCard.Ctx(ctx).
		Where("id", in.Id).
		Scan(&res)
	if err != nil {
		return nil, err
	}
	return
}
